package cn.iocoder.yudao.module.self.mq.listener;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 监听器
 */
@Configuration
public class MessageListenerConfig {
    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;

    @Autowired
    MyAckReceiver myAckReceiver;
    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerConfig(){
        SimpleMessageListenerContainer simpleMessageListenerConfig = new SimpleMessageListenerContainer(cachingConnectionFactory);
        simpleMessageListenerConfig.setConcurrentConsumers(1);
        simpleMessageListenerConfig.setMaxConcurrentConsumers(1);
        //mq默认的消息确认机制为自动 这里设置消息确认机制为手动确认消息
        simpleMessageListenerConfig.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        simpleMessageListenerConfig.setQueueNames("topic.man","topic.woman","topic.woman.other");
        simpleMessageListenerConfig.setMessageListener(new MyAckReceiver());
        return simpleMessageListenerConfig;
    }
}
